package com.wfs.suanfa.leet;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: wfs
 * @Date: 2020/3/20 15:30
 * @Description:
 */
public class LeetCode {
    public static void main(String[] args) {
        System.err.println(lengthOfLongestSubstring("nfpdmpi"));
    }

    /**
     * 寻找两个正序数组的中位数
     */
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] arr=new int[nums1.length+nums2.length];


        return 0.0;
    }


    public static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        int max = 0;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < length; i++) {
            char temp = chars[i];
            if (map.containsKey(temp)) {
                Integer index = map.get(temp);
                map.clear();
                //获取相差间隔
                int i1 = i - index;
                for (int j = index; j <= i; j++) {
                    map.put(chars[j], j);
                }
                //相差间隔则为长度
                if (i1 > max) {
                    max = i1;
                }
            } else {
                map.put(chars[i], i);
                int size = map.size();
                if (size > max) {
                    max = size;
                }
            }
        }
        return max;

    }


    /**
     * 大哥代码 学习的榜样
     */
    public int lengthOfLongestSubstringA(String s) {
        int n = s.length(),
                ans = 0;
        // current index of character
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }


}
